--
-- Author:zhangxuri
-- Date:2021-04-19
--


local plugin_name = ({...})[1]:match("^kong%.plugins%.([^%.]+)")
-- local plugin = require "kong.plugins.plugin-manage.plugin"
local plugin = require("kong.plugins." .. plugin_name .. ".plugin")
-- local tools = require "kong.plugins.plugin-manage.tools"
local tools = require("kong.plugins." .. plugin_name .. ".tools")
local kong = kong
local cjson_safe = require "cjson.safe"


--- 校验路径是否合法
-- @param file_path 待校验文件路径
local function is_legal(file_path)
  return string.find(file_path, [[^/[%w_/%-%*]+/$]]) or file_path == "/"
end


--- 发送响应
local function send_response(ok, msg)
  local ok = ok
  local msg = msg

  local res = {}
  res["message"] = msg

  if ok then
    res["code"] = plugin.HTTP_OK
  else
    res["code"] = plugin.HTTP_BAD_REQUEST
  end

  kong.response.exit(plugin.HTTP_OK,res)
end

--- 管理接口
-- @param file_path 文件路径
-- @param file_name 文件名
-- @param file_code 文件内容
return {
  ["/plugin_manage/register_plugin"] = { -- 注册插件
    GET = function(self)
      local plugin_name = self.params.plugin_name or nil

      -- 非空判断
      if not tools.is_present(plugin_name) then
        local msg = "插件名为空。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.register_plugin_exporter(plugin_name)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/unregister_plugin"] = { -- 注销插件
    GET = function(self)
      local plugin_name = self.params.plugin_name or nil

      -- 非空判断
      if not tools.is_present(plugin_name) then
        local msg = "插件名为空。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.unregister_plugin_exporter(plugin_name)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/get_plugin_list"] = { -- 获取插件列表
    GET = function(self)
      local conf_path = self.params.conf_path or plugin.get_default_conf_path()
      local conf_name = self.params.conf_name or plugin.get_default_conf_name()

      -- 非空判断
      if not tools.is_present(conf_path) then
      local msg = "配置文件路径为空,请检查插件路径配置。"
      send_response(nil, msg)
      end
      -- 非空判断
      if not tools.is_present(conf_name) then
      local msg = "配置文件路径为空,请检查插件路径配置。"
      send_response(nil, msg)
      end
      -- 合法性判断
      if not is_legal(conf_path) then
      local msg = "配置文件路径不合法,请检查插件路径配置。"
      send_response(nil, msg)
      end

      local ok, msg = plugin.get_plugin_list(conf_path, conf_name)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/add"] = { -- 新增插件
    POST = function(self)
      local plugins_path = self.params.plugins_path or plugin.get_default_plugins_path()
      local plugin_name = self.params.plugin_name or nil
      local plugin_file = self.params.plugin_file or nil

      -- 非空判断
      if not tools.is_present(plugin_name) then
        local msg = "插件名为空,请检查请求是否包含plugin_name参数。"
        send_response(nil, msg)
      end

      if not tools.is_present(plugin_file) then
        local msg = "插件文件为空,请检查请求是否包含plugin_file参数。"
        send_response(nil, msg)
      end

      -- 合法性判断
      if not is_legal(plugins_path) then
        local msg = "插件安装路径不合法,请检查插件路径配置。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.add_plugin(plugins_path, plugin_name, plugin_file)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/update_plugin"] = { -- 更新插件
    POST = function(self)
      local plugins_path = self.params.plugins_path or plugin.get_default_plugins_path()
      local plugin_name = self.params.plugin_name or nil
      local plugin_file = self.params.plugin_file or nil

      -- 非空判断
      if not tools.is_present(plugin_name) then
        local msg = "插件名为空,请检查请求是否包含plugin_name参数。"
        send_response(nil, msg)
      end

      if not tools.is_present(plugin_file) then
        local msg = "插件文件为空,请检查请求是否包含plugin_file参数。"
        send_response(nil, msg)
      end

      -- 合法性判断
      if not is_legal(plugins_path) then
        local msg = "插件安装路径不合法,请检查插件路径配置。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.update_plugin(plugins_path, plugin_name, plugin_file)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/delete_plugin"] = { -- 删除插件
    POST = function(self)
      local plugins_path = self.params.plugins_path or plugin.get_default_plugins_path()
      local plugin_name = self.params.plugin_name or nil

      -- 非空判断
      if not tools.is_present(plugin_name) then
        local msg = "插件名为空,请检查请求是否包含plugin_name参数。"
        send_response(nil, msg)
      end

      -- 合法性判断
      if not is_legal(plugins_path) then
        local msg = "插件安装路径不合法,请检查插件路径配置。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.delete_plugin(plugins_path, plugin_name)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/zip_clean"] = { -- 压缩包清理
    GET = function(self)

      local plugins_path = self.params.plugins_path or plugin.get_default_plugins_path()

      -- 非空判断
      if not tools.is_present(plugins_path) then
        local msg = "插件安装路径为空,请检查插件路径配置。"
        send_response(nil, msg)
      end

      -- 合法性判断
      if not is_legal(plugins_path) then
        local msg = "插件安装路径不合法,请检查插件路径配置。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.zip_clean(plugins_path)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/prepare_engine_config"] = { -- 检查插件配置文件
    GET = function(self)

      local conf_path = self.params.conf_path or plugin.get_default_conf_path()
      local conf_name = self.params.conf_name or plugin.get_default_conf_name()

      if not tools.is_present(conf_path) then
        local msg = "待检查的配置文件路径为空。"
        send_response(nil, msg)

      elseif not tools.is_present(conf_name) then
        local msg = "待检查的配置文件名为空。"
        send_response(nil, msg)
      end

      if not is_legal(conf_path) then
        local msg = "待检查的配置文件路径不合法。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.prepare_engine_config(conf_path, conf_name)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/reload_engine_config"] = { -- 重新加载引擎配置
    GET = function(self)

      local conf_path = self.params.conf_path or plugin.get_default_conf_path()
      local conf_name = self.params.conf_name or plugin.get_default_conf_name()

      if not tools.is_present(conf_path) then
        local msg = "待重新加载配置文件路径为空。"
        send_response(nil, msg)

      elseif not tools.is_present(conf_name) then
        local msg = "待重新加载配置文件名为空。"
        send_response(nil, msg)
      end

      if not is_legal(conf_path) then
        local msg = "待重新加载配置文件路径不合法。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.reload_engine_config(conf_path, conf_name)

      send_response(ok, msg)
    end
  },

--[[
   ["/plugin_manage/cmd"] = {
     GET = function(self)
       local cmd = self.params.cmd or nil

       local ok, msg = plugin.test_cmd(cmd)

       send_response(ok, msg)
     end
   },
  ]]

  ["/plugin_manage/get_pod_info"] = {
    GET = function(self)
      local pod_hostname = self.params.pod_hostname or nil

      local ok, msg = plugin.get_pod_info(pod_hostname)

      send_response(ok, msg)
    end
  },

  ["/plugin_manage/batch_reload_pod"] = {
    GET = function(self)
      local pod_hostname = self.params.pods_hostname or nil

      if pod_hostname then
        pod_hostname = cjson_safe.decode(pod_hostname)
        if type(pod_hostname) ~= "table" then
          local msg = "pod_hostname 类型校验不通过, 期望：json 或 字符串数组。"
          send_response(nil, msg)
        end
      end

      local ok, msg = plugin.batch_reload_pod(pod_hostname)

      send_response(ok, msg)
    end
  },

--[[
  ["/plugin_manage/file_up"] = { -- 文件
    POST = function(self)
      local plugin_name = self.params.plugin_name or nil
      local plugin_file = self.params.plugin_file or nil

      -- 非空判断
      if not tools.is_present(plugin_name) then
        local msg = "插件名为空,请检查请求是否包含plugin_name参数。"
        send_response(nil, msg)
      end

      if not tools.is_present(plugin_file) then
        local msg = "插件文件为空,请检查请求是否包含plugin_file参数。"
        send_response(nil, msg)
      end

      local ok, msg = plugin.file_up(plugin_name, plugin_file)

      send_response(ok, msg)
    end
  },
  ]]
}

